package com.lyshop.jinxiang.view;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.utils.Utils;

/**
 * Created by Administrator on 2018/1/19 0019.
 */

public class PromptDialog extends MyDialog implements View.OnClickListener {
    private Builder builder;

    private PromptDialog(Context context, Builder builder) {
        super(context, builder.isCancelAble, builder.gravity);
        builder.tv_cancel.setOnClickListener(this);
        builder.tv_confirm.setOnClickListener(this);
//        builder.iv_close.setOnClickListener(this);
        setContentView(builder.rootView);
        this.builder = builder;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                if (builder.cancelListener != null) {
                    builder.cancelListener.onCancel();
                }
                break;
            case R.id.tv_confirm:
                dismiss();
                if (builder.confirmListener != null) {
                    builder.confirmListener.onConfirm();
                }
                break;
            case R.id.iv_close:
                dismiss();
                break;
        }
    }

    public static class Builder {
        private Context mContext;
        private View rootView;
        private TextView tv_title;
        private TextView tv_promp;
        private View view_mid_line;
        private TextView tv_cancel;
        private TextView tv_confirm;
        private View iv_success;
        private View iv_close;
        private OnCancelListener cancelListener;
        private OnConfirmListener confirmListener;
        private boolean isCancelAble = false;
        private int gravity = Gravity.CENTER;

        public Builder(Context context) {
            mContext = context;
            rootView = LayoutInflater.from(context).inflate(R.layout.dialog_promp, null);
            tv_title = rootView.findViewById(R.id.tv_title);
            tv_promp = rootView.findViewById(R.id.tv_promp);
            tv_cancel = rootView.findViewById(R.id.tv_cancel);
            tv_confirm = rootView.findViewById(R.id.tv_confirm);
            view_mid_line = rootView.findViewById(R.id.view_mid_line);
//            iv_success = rootView.findViewById(R.id.iv_success);
//            iv_close = rootView.findViewById(R.id.iv_close);
//            iv_success.setVisibility(View.GONE);
//            tv_promp.setVisibility(View.GONE);
        }

        public Builder setIcon(boolean isShow) {
//            iv_success.setVisibility(isShow ? View.VISIBLE : View.GONE);
            return this;
        }

        public Builder setShowClose(boolean isShow) {
//            iv_close.setVisibility(isShow ? View.VISIBLE : View.GONE);
            return this;
        }

        public Builder setTitle(String title) {
            tv_title.setText(title);
            return this;
        }

        public Builder setContent(String content) {
            if (content != null && !content.isEmpty()) {
                Utils.setHtmlText(tv_promp, content);
                tv_promp.setVisibility(View.VISIBLE);
            }
            return this;
        }

        public Builder setButton(String left, String right) {
            tv_cancel.setText(left);
            tv_confirm.setText(right);
            return this;
        }

        public Builder setPositiveButton(String right) {
            tv_cancel.setVisibility(View.GONE);
            view_mid_line.setVisibility(View.GONE);
            tv_confirm.setText(right);
            return this;
        }

        public Builder setCancelAble(boolean isCancelAble) {
            this.isCancelAble = isCancelAble;
            return this;
        }

        public Builder setGravity(int gravity) {
            this.gravity = gravity;
            tv_promp.setGravity(gravity);
            return this;
        }

        public Builder setOnCancelListener(OnCancelListener listener) {
            this.cancelListener = listener;
            return this;
        }

        public Builder setOnConfirmListener(OnConfirmListener listener) {
            this.confirmListener = listener;
            return this;
        }

        public PromptDialog show() {
            PromptDialog dialog = new PromptDialog(mContext, this);
            dialog.show();
            return dialog;
        }
    }

    public interface OnCancelListener {
        void onCancel();
    }

    public interface OnConfirmListener {
        void onConfirm();
    }
}
